# Author: Tylor Zhang
# Email: zhangyunqiao@gmail.com

try:
  from cStringIO import StringIO
except ImportError:
  from StringIO import StringIO

from PIL import Image

def get_thumbnail(data, size):
  if hasattr(data, 'temporary_file_path'):
    file = data.temporary_file_path()
  else:
    if hasattr(data, 'read'):
      file = StringIO(data.read())
    else:
      file = StringIO(data['content'])
  image = Image.open(file)
  image.thumbnail(size)
  return image
